package net.dopan.pigframe.demo.service.impl;

import net.dopan.pigframe.core.redis.utils.RedisUtils;
import net.dopan.pigframe.demo.service.UserRedisService;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ShardedJedis;

import java.util.Set;

public class UserRedisServiceImpl implements UserRedisService {

    private String userId;
    private ShardedJedis jedis;

    public UserRedisServiceImpl(String userId) {
        this.userId = userId;
        this.jedis = RedisUtils.pool.getResource();
    }
    /**
     * 关注用户
     * @param userId
     */
    @Override
    public void follow(String userId) {
        this.jedis.sadd("graph:user"+this.userId+":following",userId);
        this.jedis.sadd("graph:user"+userId+":followed_by",this.userId);
    }

    /**
     * 取消关注
     * @param userId
     */
    @Override
    public void unfollow(String userId) {
        this.jedis.srem("graph:user"+this.userId+":following",userId);
        this.jedis.srem("graph:user"+userId+":followed_by",this.userId);
    }

    /**
     * 获取当前用户所有关注对象的userId结合
     * @return
     */
    @Override
    public Set<String> following() {
        return this.jedis.smembers("graph:user"+this.userId+":following");
    }
    /**
     * 获取关注当前用户对象的userId结合
     * @return
     */
    @Override
    public Set<String> followedBy() {
        return this.jedis.smembers("graph:user"+this.userId+":followed_by");
    }

    /**
     * 是否关注
     * @param userId
     * @return
     */
    @Override
    public boolean isfollowing(String userId) {
        return this.jedis.sismember("graph:user"+this.userId+":following",userId);
    }

    /**
     * 是否被关注
     * @param userId
     * @return
     */
    @Override
    public boolean isfollowedBy(String userId) {
        return this.jedis.sismember("graph:user"+userId+":followed_by",userId);
    }

    /**
     * 统计当前用户关注总数
     * @return
     */
    @Override
    public long followingCount() {
        return this.jedis.scard("graph:user"+this.userId+":following");
    }

    /**
     * 统计当前用户粉丝总数
     * @return
     */
    @Override
    public long followerCount() {
        return this.jedis.scard("graph:user"+userId+":followed_by");
    }

    /**
     * 共同关注
     * @param userId
     * @return
     */
    @Override
    public Set<String> commonfollowing(String userId) {
        return null;
    }

    /**
     * 共同粉丝
     * @param userId
     * @return
     */
    @Override
    public Set<String> commonfollowedBy(String userId) {
        return null;
    }
}
